package com.gdut.simple.basic;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;

import cn.bmob.v3.Bmob;

public class BasicApplication extends Application {

    private static BasicApplication application;
    private boolean isNightMode = false;

    @Override
    public void onCreate() {
        super.onCreate();
        Bmob.initialize(this, "3d51302bc213fbac6bb9a5dc4244c1e9");
        application = this;
        getAppConfig();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        setAppConfig();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        setAppConfig();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        setAppConfig();
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        setAppConfig();
    }

    public static Context getApplication() {
        return application;
    }

    private void getAppConfig() {
        SharedPreferences sp = getApplicationContext().getSharedPreferences("appConfig", MODE_PRIVATE);
        isNightMode = sp.getBoolean("isNightMode", false);
    }

    private void setAppConfig() {
        SharedPreferences sp = getApplicationContext().getSharedPreferences("appConfig", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("isNightMode", isNightMode);
        editor.apply();
    }

    public boolean isNightMode() {
        return isNightMode;
    }

    public void setNightMode(boolean isNightMode) {
        this.isNightMode = isNightMode;
    }

}
